package com.tadpole.music.presenter.study;

import com.tadpole.music.bean.study.MoNiErrorQuestionBean;
import com.tadpole.music.callback.BaseCallback;
import com.tadpole.music.config.Constant;
import com.tadpole.music.config.UrlConfig;
import com.tadpole.music.iView.study.MoNiErrorQuestionIView;
import com.tadpole.music.model.base.DataModel;
import com.tadpole.music.model.base.ModelToken;
import com.tadpole.music.presenter.base.BasePresenter;
import com.tadpole.music.utils.JsonParseUtil;
import com.tadpole.music.utils.SpUtil;
import com.tadpole.music.utils.ToastUtils;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

/**
 * 模拟错题列表
 * Created By 孙岩
 * on 2020/10/12
 */
public class MoNiErrorQuestionPresenter extends BasePresenter<MoNiErrorQuestionIView> {
    private int page = 2;
    private List<MoNiErrorQuestionBean.DataBeanX.DataBean> list;

    @SuppressWarnings("unchecked")
    public void getMoNiErrorQuestionList(int id, final boolean isRefresh) {
        String token = SpUtil.getInstance(getView().getContext()).getString(Constant.TOKEN, "");
        String url= UrlConfig.getErrorList + id + "?page=1";
        DataModel.request(ModelToken.GET_HEAD_REQUEST)
                .token(token)
                .url(url)
                .execute(new BaseCallback<String>() {
                    @Override
                    public void onSuccess(String data) {
                        try {
                            JSONObject obj = new JSONObject(data);
                            if (obj.getInt("code") == 200) {
                                JSONObject object = new JSONObject(obj.getString("data"));
                                list = new ArrayList<>();
                                JSONArray array = new JSONArray(object.getString("data"));
                                for (int i = 0; i < array.length(); i++) {
                                    JSONObject jsonObject = new JSONObject(array.get(i).toString());
                                    MoNiErrorQuestionBean.DataBeanX.DataBean bean = new MoNiErrorQuestionBean.DataBeanX.DataBean(
                                            JsonParseUtil.getInt(jsonObject, "id"),
                                            JsonParseUtil.getStr(jsonObject, "title"),
                                            JsonParseUtil.getStr(jsonObject, "err_selected")
                                    );
                                    list.add(bean);
                                }
                                if (isRefresh) {
                                    page = 2;
                                    getView().showMoNiErrorQuestionList(list);
                                    getView().notifyAdapter();
                                } else {
                                    getView().showMoNiErrorQuestionList(list);
                                }

                            } else if (obj.getInt("code") == 401) {
                                getView().show401();
                            } else {
                                ToastUtils.show(obj.getString("msg"));
                            }
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }

                    @Override
                    public void onFailure(String msg) {
                        getView().showErr();
                        getView().hideLoading();
                        getView().stopRefresh();
                    }

                    @Override
                    public void onComplete() {
                        getView().hideLoading();
                        getView().stopRefresh();
                    }

                    @Override
                    public void onBefore() {
                        getView().showLoading();
                    }
                });
    }

    @SuppressWarnings("unchecked")
    public void getMoNiErrorQuestionListMore(int id) {
        String token = SpUtil.getInstance(getView().getContext()).getString(Constant.TOKEN, "");
        String  url = UrlConfig.getErrorList + id + "?page=" + page ;
        DataModel.request(ModelToken.GET_HEAD_REQUEST)
                .token(token)
                .url(url)
                .execute(new BaseCallback<String>() {
                    @Override
                    public void onSuccess(String data) {
                        try {
                            JSONObject obj = new JSONObject(data);
                            if (obj.getInt("code") == 200) {
                                JSONObject object = new JSONObject(obj.getString("data"));
                                List<MoNiErrorQuestionBean.DataBeanX.DataBean> newList = new ArrayList<>();
                                JSONArray array = new JSONArray(object.getString("data"));
                                for (int i = 0; i < array.length(); i++) {
                                    JSONObject jsonObject = new JSONObject(array.get(i).toString());
                                    MoNiErrorQuestionBean.DataBeanX.DataBean bean = new MoNiErrorQuestionBean.DataBeanX.DataBean(
                                            JsonParseUtil.getInt(jsonObject, "id"),
                                            JsonParseUtil.getStr(jsonObject, "title"),
                                            JsonParseUtil.getStr(jsonObject, "err_selected")
                                    );
                                    newList.add(bean);
                                }
                                list.addAll(newList);
                                page++;
                                getView().notifyAdapter();

                            } else if (obj.getInt("code") == 401) {
                                getView().show401();
                            }else if(obj.getInt("code")==201){

                            }  else {
                                ToastUtils.show(obj.getString("msg"));
                            }
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }

                    @Override
                    public void onFailure(String msg) {
                        getView().showErr();
                        getView().hideLoading();
                        getView().stopRefresh();
                    }

                    @Override
                    public void onComplete() {
                        getView().hideLoading();
                        getView().stopRefresh();
                    }

                    @Override
                    public void onBefore() {
                        getView().showLoading();
                    }
                });
    }
}
